package vashion.azeroth.api.controler.cart;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.cart.obj.CartItemDO;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.personal.tran.pojo.PersonalCarts;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.tran.PersonalCartsService;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/member")
public class CartsController<T> extends BaseController {

    @Autowired
    private PersonalCartsService personalCartsService;

    /**
     * 添加购物车
     */
    @RequestMapping("addCarts.htm")
    @ResponseBody
    public ApiResult<Boolean> addCarts(
            @ModelAttribute("personalCarts") PersonalCarts personalCarts) {
        ApiResult<Boolean> ApiResult = personalCartsService
                .addCarts(personalCarts);
        return ApiResult;
    }

    /**
     * 将商品从购物车删除
     */

    @RequestMapping("removeOneItemFromCarts.htm")
    @ResponseBody
    public ApiResult<List<CartItemDO>> removeOneItemFromCarts(
            HttpServletRequest request) {

        String memberIdStr = request.getParameter("memberId");

        String cartIdStr = request.getParameter("cartId");

        if (StringUtils.isEmpty(memberIdStr) || StringUtils.isEmpty(cartIdStr)) {

            return ApiResult.newErrorResult(
                    GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
                    GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
        }

        if (!StringUtils.isNumeric(memberIdStr)
                || !StringUtils.isNumericSpace(cartIdStr)) {
            return ApiResult.newErrorResult(
                    GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
                    GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());

        }

        ApiResult<List<CartItemDO>> ApiResult = personalCartsService
                .removeOneItemFromCarts(Long.valueOf(memberIdStr),
                        Long.valueOf(cartIdStr));

        return ApiResult;
    }

    /**
     * 批量删除商品
     */

    @RequestMapping("removeMuliItemFromCarts.htm")
    @ResponseBody
    public ApiResult<List<CartItemDO>> removeMuliItemFromCarts(
            HttpServletRequest request) {

        String memberIdStr = request.getParameter("memberId");

        // 多个id中间用逗号分隔
        String cartIdListStr = request.getParameter("cartIds");

        if (StringUtils.isEmpty(memberIdStr)
                || StringUtils.isEmpty(cartIdListStr)) {

            return ApiResult.newErrorResult(
                    GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
                    GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
        }

        if (!StringUtils.isNumeric(memberIdStr) || !checkCartIds(cartIdListStr)) {
            return ApiResult.newErrorResult(
                    GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
                    GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());

        }

        List<Long> cartsIds = convertToList(cartIdListStr);

        ApiResult<List<CartItemDO>> ApiResult = personalCartsService
                .removeMuliItemFromCarts(Long.valueOf(memberIdStr), cartsIds);

        return ApiResult;
    }

    List<Long> convertToList(String cartIds) {

        if (StringUtils.isEmpty(cartIds)) {

            return null;
        }

        String[] str = cartIds.split(",");

        List<Long> cartList = new ArrayList<Long>();

        for (String s : str) {

            cartList.add(Long.valueOf(s));
        }

        return cartList;
    }

    Boolean checkCartIds(String cartIds) {

        boolean checkResult = true;

        if (StringUtils.isEmpty(cartIds)) {

            return false;
        }

        String[] str = cartIds.split(",");

        for (String cartId : str) {

            if (StringUtils.isEmpty(cartId) || !StringUtils.isNumeric(cartId)) {

                return false;
            }
        }

        return checkResult;
    }

    /**
     * 我的购物车列表
     */

    @RequestMapping("myCarts.htm")
    @ResponseBody
    public ApiResult<List<CartItemDO>> myCarts(HttpServletRequest request) {

        String memberIdStr = request.getParameter("memberId");

        if (StringUtils.isEmpty(memberIdStr)
                || !StringUtils.isNumeric(memberIdStr)) {

            return ApiResult.newErrorResult(
                    GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
                    GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
        }

        Long memberId = Long.valueOf(memberIdStr);

        ApiResult<List<CartItemDO>> ApiResult = personalCartsService
                .myCarts(memberId);

        return ApiResult;
    }

    /**
     * 购物车操作,目前只支持修改数量
     */

    @RequestMapping("operateCartItem.htm")
    @ResponseBody
    public ApiResult<Boolean> operateCartItem(
            @ModelAttribute("personalCarts") PersonalCarts personalCarts) {

        ApiResult<Boolean> ApiResult = personalCartsService
                .operateCartItem(personalCarts);
        return ApiResult;
    }

    /**
     * 创建订单后操作
     */

    @RequestMapping("dealAfterCreatOrder.htm")
    @ResponseBody
    public ApiResult<Boolean> dealAfterCreatOrder(HttpServletRequest request) {

        String memberIdStr = request.getParameter("memberId");

        // 多个id中间用逗号分隔
        String cartIdListStr = request.getParameter("cartIds");

        if (StringUtils.isEmpty(memberIdStr)
                || StringUtils.isEmpty(cartIdListStr)) {

            return ApiResult.newErrorResult(
                    GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
                    GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
        }

        if (!StringUtils.isNumeric(memberIdStr) || !checkCartIds(cartIdListStr)) {
            return ApiResult.newErrorResult(
                    GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
                    GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());

        }

        List<Long> cartsIds = convertToList(cartIdListStr);

        ApiResult<Boolean> ApiResult = personalCartsService
                .dealAfterCreatOrder(Long.valueOf(memberIdStr), cartsIds);

        return ApiResult;
    }

}
